<?php

class Pendidikan extends Controller {

	function Pendidikan() {
		parent::Controller();
        $this->load->model('Pendidikan_model', 'p');
	}
	
	function index() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}

    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_jurusan = '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,cacheLength:10,maxItemsToShow:10});'; 
    	}
		$data['additional_js'] = '
		<script type="text/javascript">
        	$(function() {
		        $("#nip").focus();'.
		        $auto_jurusan.
			'});		        
		function deleteRow(id) {
			var txt = \'Anda yakin mau menghapus data pendidikan ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
				url = "'.base_url().'index.php/pendidikan/hapus/"+id;
			 $.ajax({
			   type: "POST",
			   url: "'.base_url().'index.php/pendidikan/pendidikan_bisa_dihapus/"+id,
			   success: function(msg) {
				 if (msg==\'bisa\') {
					$.prompt(txt,{
						buttons:{Hapus:true, Batal:false},
						callback: function(v){
							if(v) {
								$("tr#row-"+id).fadeOut("slow", function() {
									$("tr#row-"+id).remove();
								});
								top.location.href=url;
							}
						}
					});
				 } else {
					alert(\'Maaf, data Pendidikan tidak dapat dihapus dikarenakan terkait dengan data lain.\');
				 }
		       }
		    });
		}
		</script>
		';
		
		$data['judul'] = 'RIWAYAT DATA PENDIDIKAN';
        if( $data['role'] == 0)
            $nip = $this->session->userdata('nip');
        else if ($this->uri->segment(3) == 'nip')
            $nip = $this->uri->segment(4);
        else
		    $nip = $this->input->post('nip')?$this->input->post('nip'):"";
        $this->session->set_userdata(array('nip' => $nip));    
        $data['list_data'] = $this->p->list_pendidikan($nip);
        $data['panel'] = $this->_setPanel($nip);
        $data['tampilkan_menu'] = true;
        
        if($this->input->post('page'))echo $data['list_data'];
        else $this->load->view('view_pendidikan',$data);
    }

    function _getJurusan() {
    	$this->db->order_by('nama', 'asc');
    	$q = $this->db->get('jurusan');
    	$list = '';
    	if ($q->num_rows() > 0) {
	    	foreach ($q->result() as $row) {
	    		$list .= '"'.$row->nama.'",';
	    	}
	    	$list = substr($list, 0, -1);
    	}
    	return $list;
    }
    
    function _getNIP() {
    	$this->db->order_by('nip', 'asc');
    	$q = $this->db->get('pegawai');
    	$list = '';
    	if ($q->num_rows() > 0) {
	    	foreach ($q->result() as $row) {
	    		$list .= '"'.$row->nip.'",';
	    	}
	    	$list = substr($list, 0, -1);
    	}
    	return $list;
    }
    
    function _setRules() {
    	return '
                    //set the rules for the field names
                    rules: {
                        nip: { required: true },
                        nama: { required: true },
                        y: { required: true },
                        nama_sekolah_universitas: { required: true },
                        nama_kepsek: { required: true }
                    },
                    //set messages to appear inline
                    messages: {
                        nip: "NIP wajib diisi",
                        nama: "Jenjang Pendidikan wajib diisi",
                        y: "Tahun Ijazah wajib diisi",
                        nama_sekolah_universitas: "Nama Sekolah/Universitas wajib diisi",
                        nama_kepsek: "Nama Kepala Sekolah/Universitas wajib diisi"
    				}
    	';
    }
    
    function _setPanel($nip) {
    	$info_pegawai = '';
		if ($nip!="") {
            $this->db->select('gelar_depan, nama, gelar_belakang');
            $this->db->where(array('nip' => $nip));
			$this->db->or_where(array('nip_baru' => $nip));
			$query = $this->db->get('pegawai');
			if ($query->num_rows() > 0) {
				$r = $query->row();
				$info_pegawai = sprintf("%s %s %s", $r->gelar_depan, $r->nama, $r->gelar_belakang);
			} else {
				$info_pegawai = '-';
			}
		}
    	$ret =  form_open('pendidikan').
				"<fieldset >
				 <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
				  <tr>
				    <td class=\"kolom\" valign=\"top\">NIP : <input type=\"text\" name=\"nip\" id=\"nip\" value=\"".$nip."\" /></td>
				    <td valign=\"top\"><input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"Tampilkan\" /></td>
				    <td valign=\"top\"><div id=\"info_pegawai\">".$info_pegawai."</div></td>
				 </tr>
				 </table>
				 </fieldset>".
				 form_close();
		return $ret;    	
    }
    
    function _setJsForm($additional_string) {
    	$ret = '
		<script type="text/javascript">
        	$(function() {
		        $("#nip").focus();'.
		        $additional_string.
                '$("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/pendidikan\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRules().'
                });
                $("form").submit(function() {
                	var nip = $("#nip").val();
			 		$.ajax({
					   type: "POST",
					   url: "'.base_url().'index.php/pendidikan/cek_nip/"+nip,
					   success: function(msg) {
						 if (msg!=\'valid\') {
						 	$.prompt(\'Maaf, NIP "\'+nip+\'" tidak terdaftar. Silahkan periksa lagi.\');
							$("#nip").focus();
							return false;
						 }
				       }
		    		});
    			});
        	});
        </script>
    	';
		return $ret;
    }
    
    function form_isian() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
        $form_action = base_url().'index.php/pendidikan/simpan';
        $form_id = 'form_pendidikan';
        
    	$list_jurusan = $this->_getJurusan();
    	if ($list_jurusan<>'') {
    		$auto_jurusan = '$("#nama_jurusan").autocompleteArray(['.$list_jurusan.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_jurusan = ''; 
    	}
    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_jurusan .= '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	}
    	
        $data['additional_js'] = $this->_setJsForm($auto_jurusan);
           
        $data['judul'] = 'INPUT DATA JENJANG PENDIDIKAN PEGAWAI';
        $data['list_data'] = $this->p->entry_pendidikan($form_id,$form_action);
        $this->load->view('view_pendidikan', $data);
    }
    
    function simpan($is_update=false, $id='') {
        $this->load->model('Datum_model', 'd');
    	if (!$this->input->post('nip')) {
            redirect('pendidikan/form_isian', 'refresh');
        } else {
            $id_pegawai = $this->d->getData('idspg_pegawai', 'pegawai', array('nip_baru' => $this->input->post('nip')));
            $arr['id_pegawai'] = $id_pegawai!=false?$id_pegawai:$this->d->getData('idspg_pegawai', 'pegawai', array('nip' => $this->input->post('nip')));
        }

        $arr['nama'] = $this->input->post('nama')?$this->input->post('nama'):"";
        $arr['nama_sekolah_universitas'] = $this->input->post('nama_sekolah_universitas')?$this->input->post('nama_sekolah_universitas'):"";
        $arr['no_ijazah'] = $this->input->post('no_ijazah')?$this->input->post('no_ijazah'):"";
        $arr['nama_kepsek'] = $this->input->post('nama_kepsek')?$this->input->post('nama_kepsek'):"";
        if ($this->input->post('d')) {
        	$a = array($this->input->post('y'), $this->input->post('m'), $this->input->post('d'));
            $arr['tgl_ijazah'] = implode(TGL_SEPARATOR, $a);
        } else {
            $arr['tgl_ijazah'] = date("Y-m-d");
        }

        if ($this->input->post('nama_jurusan')) {
        	$id_jurusan = $this->d->getData('idspg_jurusan', 'jurusan', array('nama' => $this->input->post('nama_jurusan')));
        	if ($id_jurusan) {
            	$arr['id_jurusan'] = $id_jurusan;
        	} else {
        		$this->db->insert('jurusan', array('nama' => $this->input->post('nama_jurusan')));
            	$arr['id_jurusan'] = $this->db->insert_id();
        	}
        } else {
            $arr['id_jurusan'] = 0;
        }
        
        
        	$this->p->simpan($arr, $is_update, $id);
        	redirect('pendidikan/index/nip/'.$this->input->post('nip'), 'refresh');
        
    }
    
    function form_edit($id) {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
        $this->load->model('Datum_model', 'd');
		$form_action = base_url().'index.php/pendidikan/simpan/true/'.$id;
        $form_id = 'form_pendidikan';  
        
    	$list_jurusan = $this->_getJurusan();
    	if ($list_jurusan<>'') {
    		$auto_jurusan = '$("#nama_jurusan").autocompleteArray(['.$list_jurusan.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_jurusan = ''; 
    	}
    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_jurusan .= '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	}
    	
        $data['additional_js'] = $this->_setJsForm($auto_jurusan);
        
        $this->db->where(array('idspg_pendidikan' => $id));
        $q = $this->db->get('pendidikan');
        if ($q->num_rows() > 0) {
            $row = $q->row();
            $nip2 = $this->d->getData('nip_baru', 'pegawai', array('idspg_pegawai' => $row->id_pegawai));
            $arr['nip']                  = $nip2!=''?$nip2:$this->d->getData('nip', 'pegawai', array('idspg_pegawai' => $row->id_pegawai));
            $arr['nama']                 = $row->nama;
            $arr['nama_sekolah_universitas']= $row->nama_sekolah_universitas;
            $arr['no_ijazah']            = $row->no_ijazah;
            $arr['nama_kepsek']          = $row->nama_kepsek;
            $arr['nama_jurusan']         = $this->d->getData('nama', 'jurusan', array('idspg_jurusan' => $row->id_jurusan));
            $arr['tgl_ijazah']           = $row->tgl_ijazah;
            
            $form_action = base_url().'index.php/pendidikan/simpan/true/'.$id;
            $form_id = 'form_pendidikan';
            
            $data['judul'] = 'UBAH DATA PEGAWAI';
            $data['list_data'] = $this->p->entry_pendidikan($form_id,$form_action,$arr);
            $this->load->view('view_pendidikan', $data);
        } else {
            redirect('pendidikan', 'refresh');
        }
    }


    function hapus($id) {
        $this->db->where('idspg_pendidikan', $id);
        $this->db->delete('pendidikan');
        
        redirect('pendidikan', 'refresh');
    }
    
    
    function cek_nip($nip) {
        $this->load->model('Datum_model', 'd');
        $ret = $this->d->getData('nip', 'pegawai', array('nip' => $nip));
    	$ret2 = $this->d->getData('nip_baru', 'pegawai', array('nip_baru' => $nip));
    	if ($ret!=false || $ret2!=false) {
    		echo 'valid';
    	} else {
    		echo 'ellol';
    	}
    }
    
    
    function pendidikan_bisa_dihapus($id) {
    	echo 'bisa';
    }
    
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */